<template>
  <div class="riskItem h100">
    <list-table-new
      v-bind="riskItemConfig"
      ref="riskItemTable"
    ></list-table-new>
    <dialogPublic
      :title="title"
      :show.sync="show"
      v-if="show"
      height="400"
      @doSubmit="doSubmit"
      @doClose="() => (show = false)"
    >
      <div class="h100" style="margin: 40px 60px 40px 0">
        <editForm
          :formConfig="formConfig"
          ref="editForm"
          :formData="editFormData"
        ></editForm>
      </div>
    </dialogPublic>
  </div>
</template>

<script>
import {
  queryRiskItemBySourceId,
  addRiskItem,
  updateRiskItem,
  deleteRiskItem,
} from "@/api/standardManagement/operateRiskLibrary.js";
export default {
  props: {
    riskSourceId: {
      type: String,
      default: "",
    },
    pageType: {
      type: String,
      default: "list",
    },
  },
  data() {
    return {
      show: false,
      title: "",
      riskItemConfig: {
        showIndex: true,
        selectionShow: false,
        showFilterBtn: false,
        showCustomBtn: false,
        showPagination: false,
        showSearch: false,
        searchObj: {
          searchMethod: (params, sortStr) => {
            //this.synchronizeRiskData(params, sortStr);
          },
          searchArr: [],
          queryData: {},
        },
        tableMethods: {
          rowClick: (row) => {},
        },
        actionList: [
          {
            name: "新增",
            icon: "el-icon-plus",
            disabled: !this.$store.getters.isBtnAuth("add"), //控制按钮的禁用
            content: "未授权功能", //禁用提示内容
            method: () => {
              this.addRiskItem();
            },
          },
        ],
        column: [
          {
            label: "风险描述",
            prop: "desc",
            showTooltip: true,
          },
          {
            width: 100,
            label: "风险等级",
            prop: "riskLevel",
          },
          {
            width: 100,
            label: "可能性",
            prop: "possible",
          },
          {
            width: 100,
            label: "严重性",
            prop: "serious",
          },
          {
            label: "控制措施",
            prop: "measures",
            showTooltip: true,
          },
          {
            label: "操作",
            textAlign: "center",
            type: "button",
            scopeType: "textButton",
            //width:"240px",
            buttonArr: [
              {
                text: "编辑",
                icon: "el-icon-edit",
                isShow: (row) => {
                  return true;
                },
                disabled: !this.$store.getters.isBtnAuth("edit"), //控制按钮的禁用
                content: "未授权功能", //禁用提示内容
                methods: (row) => {
                  this.editFormData = row;
                  this.title = "编辑风险项";
                  this.show = true;
                },
              },
              {
                text: "删除",
                icon: "el-icon-delete-solid",
                isShow: (row) => {
                  return true;
                },
                disabled: !this.$store.getters.isBtnAuth("delete"), //控制按钮的禁用
                content: "未授权功能", //禁用提示内容
                methods: (row) => {
                  this.$confirm("确定要删除这条数据吗", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                  })
                    .then(() => {
                      this.delete(row.id);
                    })
                    .catch(() => {});
                },
              },
            ],
          },
        ],
        tableData: [],
      },
      formConfig: {
        labelWidth: "120px",
        itemArr: [
          {
            label: "风险描述",
            prop: "desc",
            type: "textarea",
            maxlength: 100,
            span: 24,
            rows: 4,
            required: true,
          },
          {
            label: "风险等级",
            prop: "riskLevel",
            type: "select",
            span: 12,
            selectOption: this.$store.getters.dirtData("standard_risk_level"),
            required: false,
          },
          {
            label: "可能性",
            prop: "possible",
            type: "el-number",
            min: 0,
            step: 1,
            stepStrictly: true,
            span: 12,
            required: false,
          },
          {
            label: "严重性",
            prop: "serious",
            type: "el-number",
            span: 12,
            required: false,
          },
          {
            label: "控制措施",
            prop: "measures",
            type: "textarea",
            maxlength: 100,
            span: 24,
            rows: 4,
            required: false,
          },
        ],
      },
      //弹出框表单数据
      editFormData: {},
    };
  },
  created() {
    if (this.pageType != "list") {
      this.riskItemConfig.actionList = [];
      this.riskItemConfig.selectionShow = true;
      this.riskItemConfig.column[5].hide = true;
    }
  },
  watch: {
    riskSourceId: {
      handler(val) {
        this.getRiskItemData(val);
      },
    },
  },
  methods: {
    //获取风险项列表
    getRiskItemData(id) {
      queryRiskItemBySourceId({ id: id }).then((res) => {
        this.$set(this.riskItemConfig, "tableData", res.data);
      });
    },
    //新增风险项
    addRiskItem() {
      this.editFormData = {};
      this.title = "新增风险项";
      this.show = true;
    },
    //表单提交
    async doSubmit() {
      const params = await this.$refs.editForm.getFormData();
      if (this.title == "新增风险项") {
        params.sourceId = this.riskSourceId;
        addRiskItem(params).then(
          (res) => {
            if (res.code == 200) {
              this.$message.success(res.description);
              this.show = false;
              this.getRiskItemData(this.riskSourceId);
            }
          },
          (error) => {
            this.$message.error(error);
          }
        );
      } else {
        updateRiskItem(params).then(
          (res) => {
            if (res.code == 200) {
              this.$message.success(res.description);
              this.show = false;
              this.getRiskItemData(this.riskSourceId);
            }
          },
          (error) => {
            this.$message.error(error);
          }
        );
      }
    },
    //删除配置项详情
    delete(id) {
      deleteRiskItem({ id: id }).then(
        (res) => {
          if (res.code == 200) {
            this.$message.success(res.description);
            this.getRiskItemData(this.riskSourceId);
          }
        },
        (error) => {
          this.$message.error(error);
        }
      );
    },
    //获取选中的数据
    getSelectData() {
      return this.$refs.riskItemTable.getSelectData();
    },
  },
};
</script>

<style lang="less" scoped>
.riskItem {
}
</style>